/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-04-09 18:47:09
 * @LastEditors: TT
 * @LastEditTime: 2023-10-28 15:29:16
 */

import 'package:flutter/material.dart';
import 'package:hzy_project_template/base_ui_config/base_ui_config_index.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/models/normal_tool_btn_model.dart';
import 'package:hzy_project_template/pages/my/pages/my_v.dart';
import 'package:hzy_project_template/pages/root/root_tab_widget.dart';
import 'package:hzy_project_template/pages/tu_chong/pages/tu_chong_collect_list_v.dart';
import 'package:hzy_project_template/pages/tu_chong/pages/tu_chong_list_v.dart';

class RootV extends CommonGetXV<RootC> {
  RootV({Key? key}) : super(key: key);
  @override
  RootC get controller => Get.put(RootC());

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    /// 创建 body + tabbar
    Widget body = Column(
      children: [
        configTabBarView(),
        configTabBar(),
      ],
    );
    return body;
  }

  /// 创建TabbarView
  configTabBarView() {
    Widget body = TabBarView(
      controller: controller.tabController,
      physics: const NeverScrollableScrollPhysics(),
      children: controller.pageList
          .map((e) => CommonKeepAliverWrapper(child: e))
          .toList(),
    );

    body = MediaQuery.removePadding(
      context: Get.context!,
      removeBottom: true,
      child: body,
    );
    body = Expanded(child: body);
    return body;
  }

  /// 创建TabBar
  configTabBar() {
    Widget body = RootTabWidget();
    return body;
  }
}

class RootC extends CommonGetXController
    with GetSingleTickerProviderStateMixin, GetTickerProviderStateMixin {
  // --------- 属性 --------- //

  /// tab 控制器
  late TabController tabController;

  List<TabItemModel> get list => [
        TabItemModel(
          text: LaunchIdConfig.tc.tr,
          imagePath: HzyThemeImageConfig().configImagePath(
            pathkey: HzyImageId.tabWtNormal,
          ),
          selectImagePath: HzyThemeImageConfig().configImagePath(
            pathkey: HzyImageId.tabWtSelect,
          ),
        ),
        TabItemModel(
          text: LaunchIdConfig.collect.tr,
          imagePath: HzyThemeImageConfig().configImagePath(
            pathkey: HzyImageId.tabHomeNormal,
          ),
          selectImagePath: HzyThemeImageConfig().configImagePath(
            pathkey: HzyImageId.tabHomeSelect,
          ),
        ),
        TabItemModel(
          text: LaunchIdConfig.my.tr,
          imagePath: HzyThemeImageConfig().configImagePath(
            pathkey: HzyImageId.tabMyNormal,
          ),
          selectImagePath: HzyThemeImageConfig().configImagePath(
            pathkey: HzyImageId.tabMySelect,
          ),
        ),
      ];

  /// tab 界面列表
  List<Widget> pageList = [
    TuChongListV(),
    TuChongCollectListV(),
    const MyPage(),
  ];
  int selectIndex = 0;

  // --------- 生命周期  --------- //
  @override
  void onInit() {
    super.onInit();
    isShowAppBar = false;
    safeAreatop = false;

    /// 初始化 tab 控制器
    configInitController();
  }

  @override
  onClose() {
    super.onClose();
    tabController.dispose();
  }
  // --------- 数据处理  --------- //

  // --------- 触发事件  --------- //

  tapTabItem({
    required int index,
  }) {
    selectIndex = index;
    tabController.animateTo(index);
    update();
  }

  /// 初始化控制器
  configInitController() {
    /// 初始化 tab 控制器
    tabController = TabController(
      length: pageList.length,
      vsync: this,
    );
  }
}
